package com.one.common.view.widget;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;

import androidx.appcompat.widget.AppCompatEditText;

import com.one.common.R;
import com.one.common.utils.StringUtils;
import com.one.common.utils.Toaster;
import com.one.common.view.pagestate.refreshlayout.utils.DensityUtil;


/**
 * creator: zhulunjun
 * time:    2018/7/9
 * describe: 带清除功能的输入框
 */

public class ClearEditView extends AppCompatEditText implements TextWatcher{


    /** 储存清除的图片 */
    private Drawable draw;

    /**
     * 输入的文本记录
     */
    private String editSts;

    /**
     * 提示文本
     */
    private String toastStr;

    private boolean showClearTag = true;

    public ClearEditView(Context context) {
        this(context, null);
    }

    public ClearEditView(Context context, AttributeSet attrs) {
        this(context, attrs, android.R.attr.editTextStyle);
    }

    public ClearEditView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        initClearDrawable();
        this.addTextChangedListener(this);
    }

    public void setShowClearTag(boolean isShow){
        showClearTag = isShow;
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction,
                                  Rect previouslyFocusedRect) {
        // TODO Auto-generated method stub
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        // 判断焦点失去和得到时的操作
        if (focused && !this.getText().toString().equals("")) {
            this.setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[0], null, draw, null);
        } else {
            this.setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[0], null, null, null);
        }
    }


    /**
     * 初始化清除的图片
     */
    private void initClearDrawable() {
        draw = getCompoundDrawables()[2];

        // 判断清除的图片是否为空
        if (draw == null) {
            draw = getResources().getDrawable(R.mipmap.ic_et_clear);
        }

        // 将输入框默认设置为没有清除的按钮
        this.setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[0], null, null, null);
    }

    public void onTextChanged(CharSequence text, int start, int lengthBefore,
                              int lengthAfter) {
        // 判断输入框中是否有内容
        if (text.length() > 0 && showClearTag) {
            this.setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[0], null, draw, null);
            this.setCompoundDrawablePadding(DensityUtil.dp2px(getContext(), 12));
        } else {
            this.setCompoundDrawablesWithIntrinsicBounds(getCompoundDrawables()[0], null, null, null);
        }
        if(textChangeListener!=null){
            textChangeListener.change(text);
        }
        // 如果需要限制只能输入两位小数
        if(StringUtils.isNotBlank(toastStr)){
            handleTwoDecimal(text);
        }
    }

    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {

    }

    public void afterTextChanged(Editable s) {

    }

    public void setToastStr(String toastStr) {
        this.toastStr = toastStr;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // 判断触碰是否结束
        if (event.getAction() == MotionEvent.ACTION_UP) {
            // 判断所触碰的位置是否为清除的按钮
            if (event.getX() > (getWidth() - getTotalPaddingRight())
                    && event.getX() < (getWidth() - getPaddingRight())) {
                // 将editText里面的内容清除
                setText("");
            }
        }
        return super.onTouchEvent(event);
    }

    private TextChangeListener textChangeListener;

    public void setTextChangeListener(TextChangeListener textChangeListener) {
        this.textChangeListener = textChangeListener;
    }

    public interface TextChangeListener{
        void change(CharSequence str);
    }

    /**
     * 只保留两位小数的处理
     */
    private void handleTwoDecimal(CharSequence charSequence){
        if (StringUtils.isNotBlank(charSequence.toString())) {
            // 单车货值 两位小数
            int index = charSequence.toString().indexOf(".");
            boolean isPast = true;
            if (index > 0) {
                String goodsValues = charSequence.toString().substring(index);
                if (StringUtils.isNotBlank(goodsValues) && goodsValues.length() > 3) {
                    Toaster.showShortToast(toastStr);
                    isPast = false;
                }
            }
            if(isPast){
                editSts = charSequence.toString();
            }else {
                if(!editSts.equals(".")) {
                    this.setText(editSts);
                    endFocus();
                }
            }
        }
    }

    public void endFocus(){
        this.requestFocus();
        this.setSelection(getText().length());
    }
}
